package com.yanqu.road.dao.impl.activity.shushan;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.shushan.ShuShanLiLianGridData;
import com.yanqu.road.entity.activity.shushan.ShuShanLiLianRecordInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description
 * @Author cwq
 * @Data 2022/3/18 16:52
 */
public class ShuShanLiLianGridDataDaoImpl extends TempDataObjectDao<ShuShanLiLianGridData> {
    public static String getShuShanLiLianGridDataSql = "select * from `t_u_activity_shushan_lilian_grid_data` where `activityId`=? ";

    public Map<Long, Map<Integer, ShuShanLiLianGridData>> getShuShanLiLianGridDataMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, Map<Integer, ShuShanLiLianGridData>> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getShuShanLiLianGridDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ShuShanLiLianGridData item = parse(rs);
                Map<Integer, ShuShanLiLianGridData> gridDataMap = map.get(item.getUserId());
                if (gridDataMap == null) {
                    gridDataMap = new ConcurrentHashMap<>();
                    map.put(item.getUserId(), gridDataMap);
                }
                gridDataMap.put(item.getGridId(), item);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateShuShanLiLianGridDataSql = "update `t_u_activity_shushan_lilian_grid_data` set curDiscipleId=?,lastFinishId=?,remainTime=?,endTime=?,receivedId=?,recordInfoList=? where activityId=? and userId=? and gridId=?";

    @Override
    protected boolean updateData(ShuShanLiLianGridData shuShanLiLianGridData) {
        shuShanLiLianGridData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, shuShanLiLianGridData.getCurDiscipleId()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanLiLianGridData.getLastFinishId()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanLiLianGridData.getRemainTime()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanLiLianGridData.getEndTime()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanLiLianGridData.getReceivedId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(shuShanLiLianGridData.getRecordInfoList())));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanLiLianGridData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, shuShanLiLianGridData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanLiLianGridData.getGridId()));
        if (getDb().executeNoneQuery(updateShuShanLiLianGridDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertShuShanLiLianGridDataSql = "insert into `t_u_activity_shushan_lilian_grid_data`(activityId,userId,gridId,curDiscipleId,lastFinishId,remainTime,endTime,receivedId,recordInfoList) values (?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(ShuShanLiLianGridData shuShanLiLianGridData) {
        shuShanLiLianGridData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, shuShanLiLianGridData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, shuShanLiLianGridData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanLiLianGridData.getGridId()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanLiLianGridData.getCurDiscipleId()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanLiLianGridData.getLastFinishId()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanLiLianGridData.getRemainTime()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanLiLianGridData.getEndTime()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanLiLianGridData.getReceivedId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(shuShanLiLianGridData.getRecordInfoList())));
        if (getDb().executeNoneQuery(insertShuShanLiLianGridDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected ShuShanLiLianGridData parseData(ResultSet rs) throws SQLException {
        ShuShanLiLianGridData shuShanLiLianGridData = new ShuShanLiLianGridData();
        shuShanLiLianGridData.setActivityId(rs.getInt("activityId"));
        shuShanLiLianGridData.setUserId(rs.getLong("userId"));
        shuShanLiLianGridData.setGridId(rs.getInt("gridId"));
        shuShanLiLianGridData.setCurDiscipleId(rs.getInt("curDiscipleId"));
        shuShanLiLianGridData.setLastFinishId(rs.getInt("lastFinishId"));
        shuShanLiLianGridData.setRemainTime(rs.getInt("remainTime"));
        shuShanLiLianGridData.setEndTime(rs.getInt("endTime"));
        shuShanLiLianGridData.setReceivedId(rs.getInt("receivedId"));
        shuShanLiLianGridData.setRecordInfoList(JSON.parseObject(rs.getString("recordInfoList"), new TypeReference<List<ShuShanLiLianRecordInfo>>() {
        }));

        return shuShanLiLianGridData;
    }
}
